#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
class duoxiangshi
{
    public:
        struct node
        {
            int xi,ci;
            node *next,*front;
        }*head,*end;
        duoxiangshi();
        void insert(int xi,int ci);
        void delete_node(int ci);
        duoxiangshi operator+(const duoxiangshi &b)
        {
            duoxiangshi plus;
            node *p1=head->next,*p2=b.head->next;
            while(p1!=NULL&&p2!=NULL)
            {
                if(p1->ci>p2->ci)
                {
                    plus.insert(p1->xi,p1->ci);
                    p1=p1->next;
                }
                else if(p1->ci<p2->ci)
                {
                    plus.insert(p2->xi,p2->ci);
                    p2=p2->next;
                }
                else {
                    plus.insert(p1->xi+p2->xi,p1->ci);
                    p1=p1->next;
                    p2=p2->next;
                }
            }
            if(p1!=NULL)
            {
                while(p1!=NULL)
                {
                    plus.insert(p1->xi,p1->ci);
                    p1=p1->next;
                }
            }
            else if(p2!=NULL)
            {
                while(p2!=NULL)
                {
                    plus.insert(p2->xi,p2->ci);
                    p2=p2->next;
                }
            }
            return plus;
        }
        void print()
        {
            node *p=head->next;
            while(p->next!=NULL)
            {
                cout<<p->xi<<"x^"<<p->ci<<"+";
                p=p->next;
            }
            cout<<p->xi<<"x^"<<p->ci<<endl;
        }
};
duoxiangshi::duoxiangshi()
{
    head = new node;
    head->xi=head->ci=0;
    head->next=head->front=NULL;
    end=head;
}
void duoxiangshi::insert(int xi,int ci)
{
    node *p=new node;//在尾部插入，但这里没有考虑按照ci排序
    p->xi=xi;
    p->ci=ci;
    end->next=p;
    p->front=end;
    end=p;
}
void duoxiangshi::delete_node(int ci)
{
    node *p=head->next;
    while(p!=NULL&&p->ci!=ci)
    {
        p=p->next;
    }
    if(p!=NULL)
    {
        p->front->next=p->next;
        if(p->next!=NULL)p->next->front=p->front;
            else end=p->front;
        delete p;
    }
}
struct node1 
    {
        int xi,ci;
    }a0[1005];
bool cmp(node1 a,node1 b)
{
    if(a.ci>b.ci)return true;
    else return false;
}
int main()
{
    duoxiangshi d1,d2;
    int n1,n2;
    cin>>n1>>n2;
    for(int i=1;i<=n1;i++)cin>>a0[i].xi>>a0[i].ci;
    sort(a0+1,a0+n1+1,cmp);
    for(int i=1;i<=n1;i++)d1.insert(a0[i].xi,a0[i].ci);
    memset(a0,0,sizeof(a0));
    for(int i=1;i<=n2;i++)cin>>a0[i].xi>>a0[i].ci;
    sort(a0+1,a0+n2+1,cmp);
    for(int i=1;i<=n2;i++)d2.insert(a0[i].xi,a0[i].ci);
    duoxiangshi d3=d1+d2;
    d3.print();
    return 0;

}